/**
 * 功能描述
 */

define(function (require, exports, module) {
	var Grid = require("grid");
	var aw = require("ajaxwrapper");
	
    var instance = {
        initComponent: function (params,widget) {
        	var grid = new Grid({
        		parentNode: ".J-grid",
        		//autoRender: false,
        		model: {
        			url: "api/returncredentials/query",
        			params: function(){
        				var subnav = widget.get("subnav");
        				return {
        					 "flowStatus": subnav.getValue("status"),
        				     "operateDate": subnav.getValue("startDate").start,
        				     "operateDateEnd": subnav.getValue("startDate").end,
        				     fetchProperties: widget.constant.fetchProperties
        				}
        			},
        			columns: [{
        				name: "travelContract.memberCertificates",
        				label: "会员证号码",
        				format: "detail",
        				formatparams: {
        					key: "detail",
        					handler: function(index,data,rowEle){
        						var form = widget.get("form");
        						form.setDisabled(true);
        						var memberCertificates = data.travelContract.memberCertificates;
								if(memberCertificates){
									form.setValue("memberCertificates",data.travelContract.memberCertificates);
									form.setValue("pkReturnCredentials",data.pkReturnCredentials);
									form.setValue("defaultRatio",data.defaultRatio);
								    form.setValue("flatCost",data.flatCost);
									form.setValue("cause",data.cause);
									form.setValue("price",data.price);
									form.setValue("version",data.version);
									//加载审批流
		        					var approvalUI  = widget.get("approvalUI");
									approvalUI.set("param",{
										modelId:data.pkReturnCredentials,
										serviceType:"ReturnCredentials",
										hideButton:true
									});
									approvalUI.get("appgrid").refresh();
									widget.hide([".J-grid"]).show([".J-form",".J-coupongrid",".J-applicategrid"]);
									widget.get("subnav").hide(["status","startDate","search","add","save"]).show(["return"]);
									widget.handler.form.searchContact(widget,memberCertificates,data.pkReturnCredentials,false);
								}
        					}
        				}
        			},{
        				name: "travelContract.contactNo",
        				label: "合同号",
        			},{
        				name: "travelContract.member.personalInfo.name",
        				label: "会员",
        			},{
        				name: "travelContract.member.personalInfo.idNumber",
        				label: "证件号",
        			},{
        				name: "travelContract.productRule.product.name",
        				label: "产品",
        			},{
        				name: "travelContract.productType.name",
        				label: "产品类型",
        			},{
        				name: "travelContract.number",
        				label: "份数",
        			},{
        				name: "operateDate",
        				label: "申请日期",
        				format: "date"
        			},{
        				name: "price",
        				label: "应退金额(元)",
        				format: function(value,row){
        					if(value){
								return value.toFixed(2);
							}else{
								return ""
							}
        				}
        			},{
        				name: "flowStatus.value",
        				label: "状态",
        			},{
        				name: "operate",
        				label: "操作",
        				format: "button",
        				formatparams: [{
        					id: "edit",
        					icon: "icon-edit",
        					show: function(value,data){
        						if(data.flowStatus.key == "Initial" || data.flowStatus.key == "NotApproved"){
        							return true;
        						}else{
        							return false;
        						}
        					},
        					handler: function(index,data,rowEle){
        						var form = widget.get("form");
        						var memberCertificates = data.travelContract.memberCertificates;
								if(memberCertificates){
									form.setValue("memberCertificates",data.travelContract.memberCertificates);
									form.setValue("pkReturnCredentials",data.pkReturnCredentials);
									form.setValue("defaultRatio",data.defaultRatio);
								    form.setValue("flatCost",data.flatCost);
									form.setValue("cause",data.cause);
									form.setValue("price",data.price);
									form.setValue("version",data.version);
									widget.hide([".J-grid"]).show([".J-form",".J-coupongrid"]);
									widget.get("subnav").hide(["status","startDate","search","add"]).show(["return","save"]);
									widget.handler.form.searchContact(widget,memberCertificates,null,false);
								}
        					}
        				},{
        					id: "delete",
        					icon: "icon-remove",
        					show: function(value,data){
        						if(data.flowStatus.key == "Initial" || data.flowStatus.key == "NotApproved"){
        							return true;
        						}else{
        							return false;
        						}
        					},
        					handler: function(index,data,rowEle){
        						aw.del("api/returncredentials/"+data.pkReturnCredentials+"/delete",function(){
        							grid.refresh();
        						});
        					}
        				},{
        					id: "commit",
        					text: "提交",
        					show: function(value,data){
        						if(data.flowStatus.key == "Initial" || data.flowStatus.key == "NotApproved"){
        							return true;
        						}else{
        							return false;
        						}
        					},
        					handler:function(index,data,rowEle){
        						var form = widget.get("form");
        						var pkReturnCredentials = data.pkReturnCredentials;
        						if(pkReturnCredentials){
        							form.setValue("pkReturnCredentials",data.pkReturnCredentials);
									form.setValue("defaultRatio",data.defaultRatio);
								    form.setValue("flatCost",data.flatCost);
									form.setValue("cause",data.cause);
									form.setValue("price",data.price);
        							widget.handler.grid.statusChange(widget,pkReturnCredentials,null);
        						}
        					}
        				}]
        			}]
        		}
        	});
        	widget.set("grid",grid)
        }
    };
    module.exports = instance;
});